package com.qfjg.webcore.log.worker;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;

public class TaskQueueWorker<T> {
    private ConcurrentLinkedQueue<T> clq;
    private IWorkAction<T> action;
    public TaskQueueWorker(IWorkAction<T> action){
        this.action=action;
        this.clq=new ConcurrentLinkedQueue<>();
        startWorkerTimer();
    }

    private void startWorkerTimer(){
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    T t = clq.poll();
                    while (t!=null){
                        action.Do(t);
                        t = clq.poll();
                    }
                }
                catch (Exception ex){
                    System.out.println("工作任务执行时出错："+ex);
                }
            }
        },0,1000);
    }

    public void AddTask(T t){
        clq.add(t);
    }


}
